home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / invest / applet.py < prev    next >
Text File  |  2009-10-20  |  5KB  |  182 lines

  1. import os, time
  2. from os.path import *
  3. import gnomeapplet, gtk, gtk.gdk, gconf, gobject
  4. gobject.threads_init()
  5. from gettext import gettext as _
  6. import gconf
  7.  
  8. import invest, invest.about, invest.chart, invest.preferences, invest.defs
  9. from invest.quotes import QuoteUpdater
  10. from invest.widgets import *
  11.  
  12. gtk.window_set_default_icon_from_file(join(invest.ART_DATA_DIR, "invest_neutral.svg"))
  13.  
  14. class InvestApplet:
  15.     def __init__(self, applet):
  16.         self.applet = applet
  17.         self.applet.setup_menu_from_file (
  18.             None, "Invest_Applet.xml",
  19.             None, [("About", self.on_about), 
  20.                     ("Prefs", self.on_preferences),
  21.                     ("Refresh", self.on_refresh)
  22.                     ])
  23.  
  24.         evbox = gtk.HBox()
  25.         self.applet_icon = gtk.Image()
  26.         self.set_applet_icon(0)
  27.         self.applet_icon.show()
  28.         evbox.add(self.applet_icon)
  29.         self.applet.add(evbox)
  30.         self.applet.connect("button-press-event",self.button_clicked)
  31.         self.applet.show_all()
  32.         self.new_ilw()
  33.  
  34.     def new_ilw(self):
  35.         self.quotes_updater = QuoteUpdater(self.set_applet_icon,
  36.                            self.set_applet_tooltip)
  37.         self.investwidget = InvestWidget(self.quotes_updater)
  38.         self.ilw = InvestmentsListWindow(self.applet, self.investwidget)
  39.  
  40.     def reload_ilw(self):
  41.         self.ilw.destroy()
  42.         self.new_ilw()
  43.  
  44.     def button_clicked(self, widget,event):
  45.         if event.type == gtk.gdk.BUTTON_PRESS and event.button == 1:
  46.             # Three cases...
  47.             if len (invest.STOCKS) == 0:
  48.                 # a) We aren't configured yet
  49.                 invest.preferences.show_preferences(self, _("<b>You have not entered any stock information yet</b>"))
  50.                 self.reload_ilw()
  51.             elif not self.quotes_updater.quotes_valid:
  52.                 # b) We can't get the data (e.g. offline)
  53.                 alert = gtk.MessageDialog(buttons=gtk.BUTTONS_CLOSE)
  54.                 alert.set_markup(_("<b>No stock quotes are currently available</b>"))
  55.                 alert.format_secondary_text(_("The server could not be contacted. The computer is either offline or the servers are down. Try again later."))
  56.                 alert.run()
  57.                 alert.destroy()
  58.             else:
  59.                 # c) Everything is normal: pop-up the window
  60.                 self.ilw.toggle_show()
  61.     
  62.     def on_about(self, component, verb):
  63.         invest.about.show_about()
  64.     
  65.     def on_preferences(self, component, verb):
  66.         invest.preferences.show_preferences(self)
  67.         self.reload_ilw()
  68.     
  69.     def on_refresh(self, component, verb):
  70.         self.quotes_updater.refresh()
  71.  
  72.     def set_applet_icon(self, change):
  73.         if change == 1:
  74.             pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(join(invest.ART_DATA_DIR, "invest-22_up.png"), -1,-1)
  75.         elif change == 0:
  76.             pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(join(invest.ART_DATA_DIR, "invest-22_neutral.png"), -1,-1)
  77.         else:
  78.             pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(join(invest.ART_DATA_DIR, "invest-22_down.png"), -1,-1)
  79.         self.applet_icon.set_from_pixbuf(pixbuf)
  80.     
  81.     def set_applet_tooltip(self, text):
  82.         self.applet_icon.set_tooltip_text(text)
  83.  
  84. class InvestmentsListWindow(gtk.Window):
  85.     def __init__(self, applet, list):
  86.         gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
  87.         self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DOCK)
  88.         self.stick()
  89.         self.set_resizable(False)
  90.         self.set_border_width(6)
  91.         
  92.         self.applet = applet # this is the widget we want to align with
  93.         self.alignment = self.applet.get_orient ()
  94.         
  95.         self.add(list)
  96.         list.show()
  97.  
  98.         # boolean variable that identifies if the window is visible
  99.         # show/hide is triggered by left-clicking on the applet
  100.         self.hidden = True
  101.  
  102.     def toggle_show(self):
  103.         if self.hidden == True:
  104.             self.update_position()
  105.             self.show()
  106.             self.hidden = False
  107.         elif self.hidden == False:
  108.             self.hide()
  109.             self.hidden = True
  110.  
  111.     def update_position (self):
  112.         """
  113.         Calculates the position and moves the window to it.
  114.         """
  115.         self.realize()
  116.  
  117.         # Get our own dimensions & position
  118.         #(wx, wy) = self.get_origin()
  119.         (ax, ay) = self.applet.window.get_origin()
  120.  
  121.         (ww, wh) = self.get_size ()
  122.         (aw, ah) = self.applet.window.get_size ()
  123.  
  124.         screen = self.applet.window.get_screen()
  125.         monitor = screen.get_monitor_geometry (screen.get_monitor_at_window (self.applet.window))
  126.  
  127.         if self.alignment == gnomeapplet.ORIENT_LEFT:
  128.                 x = ax - ww
  129.                 y = ay
  130.  
  131.                 if (y + wh > monitor.y + monitor.height):
  132.                     y = monitor.y + monitor.height - wh
  133.  
  134.                 if (y < 0):
  135.                     y = 0
  136.                 
  137.                 if (y + wh > monitor.height / 2):
  138.                     gravity = gtk.gdk.GRAVITY_SOUTH_WEST
  139.                 else:
  140.                     gravity = gtk.gdk.GRAVITY_NORTH_WEST
  141.                     
  142.         elif self.alignment == gnomeapplet.ORIENT_RIGHT:
  143.                 x = ax + aw
  144.                 y = ay
  145.  
  146.                 if (y + wh > monitor.y + monitor.height):
  147.                     y = monitor.y + monitor.height - wh
  148.                 
  149.                 if (y < 0):
  150.                     y = 0
  151.                 
  152.                 if (y + wh > monitor.height / 2):
  153.                     gravity = gtk.gdk.GRAVITY_SOUTH_EAST
  154.                 else:
  155.                     gravity = gtk.gdk.GRAVITY_NORTH_EAST
  156.  
  157.         elif self.alignment == gnomeapplet.ORIENT_DOWN:
  158.                 x = ax
  159.                 y = ay + ah
  160.  
  161.                 if (x + ww > monitor.x + monitor.width):
  162.                     x = monitor.x + monitor.width - ww
  163.  
  164.                 if (x < 0):
  165.                     x = 0
  166.  
  167.                 gravity = gtk.gdk.GRAVITY_NORTH_WEST
  168.         elif self.alignment == gnomeapplet.ORIENT_UP:
  169.                 x = ax
  170.                 y = ay - wh
  171.  
  172.                 if (x + ww > monitor.x + monitor.width):
  173.                     x = monitor.x + monitor.width - ww
  174.  
  175.                 if (x < 0):
  176.                     x = 0
  177.  
  178.                 gravity = gtk.gdk.GRAVITY_SOUTH_WEST
  179.  
  180.         self.move(x, y)
  181.         self.set_gravity(gravity)
  182.